package com.service.business.base.service.impl;

import com.google.common.collect.Maps;
import com.service.business.base.enums.AreaEnum;
import com.service.dao.mapper.SysAreaMapper;
import com.service.business.base.service.AreaService;
import com.service.business.support.Language;
import com.service.dao.model.SysArea;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年05月12日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private SysAreaMapper areaMapper;

    @Override
    public Map<String, Object> getCountryArea() {
        switch (Language.LANG.get()) {
            case "zh_CN":
                return this.getCNCountryArea();
            default:
                return null;
        }
    }

    public Map<String, Object> getCNCountryArea() {
        Map<String, Object> resultMap = Maps.newHashMap();
        resultMap.put("province_list", buildDataMap(AreaEnum.PROVINCE));
        resultMap.put("city_list", buildDataMap(AreaEnum.CITY));
        resultMap.put("county_list", buildDataMap(AreaEnum.COUNTY));
        return resultMap;
    }

    private Map<Integer, String> buildDataMap(AreaEnum areaEnum) {
        List<SysArea> areaList = areaMapper.listByCountryAndStatus(Language.LANG.get(), areaEnum.getValue());
        if (ObjectUtils.isEmpty(areaList)) {
            return null;
        }
        Map<Integer, String> resultMap = Maps.newLinkedHashMap();
        for (SysArea item : areaList) {
            resultMap.put(item.getId(), item.getAreaName());
        }
        return resultMap;
    }

}
